package com.shixun.controller;


import com.shixun.dto.JsonDto;
import com.shixun.pojo.TbBusProblem;
import com.shixun.pojo.TbRepairRecord;
import com.shixun.service.BusManageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController("/api/BusManage")
public class BusManageController {

    @Autowired
    BusManageService busManageService;

    @PostMapping("/insertBusInfo")
    public Map<String, Object> addOrder(HttpServletRequest req, HttpServletResponse resp){
        try {
            req.setCharacterEncoding("UTF-8");
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        }
        resp.setCharacterEncoding("UTF-8");

//        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
//        AdminInfoService adminInfoService = context.getBean(AdminInfoService.class);

        String busName = req.getParameter("busName");
        int busStatus = Integer.parseInt(req.getParameter("busStatus"));
        int repairDriverId = Integer.parseInt(req.getParameter("repairDriverId"));
        String city = req.getParameter("city");



        Integer flag = busManageService.insertBusInfo(busName,busStatus,repairDriverId,city);

        Map<String, Object> result = new HashMap<>();
        result.put("success", flag != null && flag > 0);

        System.out.println("111111111" + result);
        return result;
    }

    @PostMapping("/modifyBusInfo")
    public JsonDto modifyBusInfo(HttpServletRequest req, HttpServletResponse resp){
        try {
            req.setCharacterEncoding("UTF-8");
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        }
        resp.setCharacterEncoding("UTF-8");

        String newBusName = req.getParameter("newBusName");
        int newBusStatus = Integer.parseInt(req.getParameter("newBusStatus"));
        int newRepairDriver = Integer.parseInt(req.getParameter("newRepairDriver"));
        String newCity = req.getParameter("newCity");



        JsonDto jsonDto = new JsonDto();
        boolean updateResult = busManageService.modifyBusInfo(newBusName,newBusStatus,newRepairDriver,newCity);
        Map<String, Object> datas = new HashMap<>();
        datas.put("updateResult", updateResult);
        jsonDto.setId(1);
        jsonDto.setDatas(datas);
        return jsonDto;
    }


    @PostMapping("/selectBrokenBus")
    public JsonDto selectBrokenBus(HttpServletRequest req, HttpServletResponse resp){
        JsonDto jsonDto = new JsonDto();
        try {
            req.setCharacterEncoding("UTF-8");
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        }
        resp.setCharacterEncoding("UTF-8");

        int busId = Integer.parseInt(req.getParameter("busId"));

        System.out.println("1111111111111name="+busId);

        List<TbBusProblem> busProblemList = busManageService.selectBrokenBus(busId);
        if (busProblemList.size()>0){
            jsonDto.setId(1);
            jsonDto.getDatas().put("problemList", busProblemList);
        }
        return jsonDto;
    }

    @PostMapping("/setStatus1")
    public JsonDto setStatus1(HttpServletRequest req, HttpServletResponse resp){
        try {
            req.setCharacterEncoding("UTF-8");
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        }
        resp.setCharacterEncoding("UTF-8");


        int busId = Integer.parseInt(req.getParameter("busId"));
        JsonDto jsonDto = new JsonDto();
        boolean updateResult = busManageService.setStatus1(busId);
        Map<String, Object> datas = new HashMap<>();
        datas.put("updateResult", updateResult);
        jsonDto.setId(1);
        jsonDto.setDatas(datas);
        return jsonDto;
    }

    @PostMapping("/setStatus3")
    public JsonDto setStatus3(HttpServletRequest req, HttpServletResponse resp){
        try {
            req.setCharacterEncoding("UTF-8");
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        }
        resp.setCharacterEncoding("UTF-8");


        int busId = Integer.parseInt(req.getParameter("busId"));
        JsonDto jsonDto = new JsonDto();
        boolean updateResult = busManageService.setStatus3(busId);
        Map<String, Object> datas = new HashMap<>();
        datas.put("updateResult", updateResult);
        jsonDto.setId(1);
        jsonDto.setDatas(datas);
        return jsonDto;
    }

    @PostMapping("/setStatus4")
    public JsonDto updateStatus4(HttpServletRequest req, HttpServletResponse resp){
        try {
            req.setCharacterEncoding("UTF-8");
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        }
        resp.setCharacterEncoding("UTF-8");


        int busId = Integer.parseInt(req.getParameter("busId"));
        JsonDto jsonDto = new JsonDto();
        boolean updateResult = busManageService.setStatus4(busId);
        Map<String, Object> datas = new HashMap<>();
        datas.put("updateResult", updateResult);
        jsonDto.setId(1);
        jsonDto.setDatas(datas);
        return jsonDto;
    }

    @PostMapping("/selectRepairBus")
    public JsonDto selectRepairBus(HttpServletRequest req, HttpServletResponse resp){
        JsonDto jsonDto = new JsonDto();
        try {
            req.setCharacterEncoding("UTF-8");
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        }
        resp.setCharacterEncoding("UTF-8");

        int busId = Integer.parseInt(req.getParameter("busId"));

        System.out.println("1111111111111name="+busId);

        List<TbRepairRecord> busRepairList = busManageService.selectRepairBus(busId);
        if (busRepairList.size()>0){
            jsonDto.setId(1);
            jsonDto.getDatas().put("repairList", busRepairList);
        }
        return jsonDto;
    }
}
